package com.cn.kabalifeuser.view.dialogView;//package com.cn.automobileservice.view.dialogView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.ui.service.adapter.TypeAdapter;
import com.cn.kabalifeuser.ui.service.bean.IndexTypeListBean;
import com.cn.kabalifeuser.utils.StringUtil;
import com.flyco.roundview.RoundTextView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 晓林 on 2019/10/18.
 * function: 首页。提示框。是否是大掌柜，。
 */
public class ApplyShopDialog extends Dialog {
    protected Context mContext;

    protected WindowManager.LayoutParams mLayoutParams;


    RoundTextView tv_btn_next;

    RecyclerView rv_list;
    ListAdapter listAdapter;
    List<IndexTypeListBean.ResultBean> mList;

    public RoundTextView getTv_btn_next() {
        return tv_btn_next;
    }

    public ApplyShopDialog(Context context, List<IndexTypeListBean.ResultBean> mList) {
        super(context);
        this.mList = mList;
        initView(context, mList);
    }

    public ApplyShopDialog(Activity context, List<IndexTypeListBean.ResultBean> mList) {
        super(context);
        this.mList = mList;
        initView(context, mList);
    }


    private void initView(Context context, List<IndexTypeListBean.ResultBean> mList) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setBackgroundDrawableResource(R.drawable.transparent_bg);
        mContext = context;
        Window window = this.getWindow();
        mLayoutParams = window.getAttributes();
        mLayoutParams.alpha = 1f;
        window.setAttributes(mLayoutParams);
        if (mLayoutParams != null) {
            mLayoutParams.height = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
            mLayoutParams.gravity = Gravity.CENTER;
        }
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_apply_shop, null);
        tv_btn_next = dialogView.findViewById(R.id.tv_btn_next);
        rv_list = dialogView.findViewById(R.id.rv_list);

        rv_list.setLayoutManager(new GridLayoutManager(context, 3));
        rv_list.setNestedScrollingEnabled(false);
        //创建适配器
        listAdapter = new ListAdapter(R.layout.d_main_item, mList);
        //给RecyclerView设置适配器
        rv_list.setAdapter(listAdapter);
        listAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < mList.size(); i++) {
                    if (i == position) {
                        if (mList.get(i).isCheck())
                            mList.get(i).setCheck(false);
                        else mList.get(i).setCheck(true);
                    }
                }
                listAdapter.notifyDataSetChanged();
            }
        });

        setContentView(dialogView);

    }


    public String setCheCkStr() {
        List<String> lsitStr = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).isCheck())
                lsitStr.add(String.valueOf(mList.get(i).getId()));
        }
        return StringUtil.convertListToString(lsitStr);
    }


    class ListAdapter extends BaseQuickAdapter<IndexTypeListBean.ResultBean, BaseViewHolder> {

        public ListAdapter(int layoutResId, @Nullable List<IndexTypeListBean.ResultBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final IndexTypeListBean.ResultBean item) {
            RoundTextView tvname = helper.getView(R.id.tv_title);
            tvname.setText(item.getTitle());
            if (item.isCheck()) {
                tvname.setTextColor(mContext.getResources().getColor(R.color.main_white));
                tvname.getDelegate().setBackgroundColor(mContext.getResources().getColor(R.color.main_color));
            } else {
                tvname.setTextColor(mContext.getResources().getColor(R.color.text_909090));
                tvname.getDelegate().setBackgroundColor(mContext.getResources().getColor(R.color.text_f8f8f8));
            }
        }

    }
}
